package com.yue.jpa.service;

import com.yue.jpa.entity.*;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.List;

/**
 * 功能描述: TODO <br/>
 * 创建时间: 2016/9/2<br/>
 *
 * @author QiuYang
 */
public class EmployeeService {
    protected EntityManager entityManager;

    public EmployeeService(EntityManager entityManager){
        this.entityManager = entityManager;
    }

    public Employee create(Employee employee) {
        entityManager.persist(employee);
        return employee;
    }

    public Employee createEmployee(int id, String name, double salary){
        Employee employee = new Employee(id);
        employee.setName(name);
        employee.setSalary(salary);

        entityManager.persist(employee);
        return employee;
    }

    public void removeEmployee(int id){
        Employee employee = findEmployee(id);
        if (employee != null) {
            entityManager.remove(employee);
        }
    }

    public Employee raiseEmployeeSalary(int id, double raise){
        Employee employee = findEmployee(id);
        if(employee != null){
            employee.setSalary(employee.getSalary() + raise);
        }

        return  employee;
    }

    public Employee findEmployee(int id) {
        return entityManager.find(Employee.class, id);
    }

    public List<Employee> findAllEmployees(){
        TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e", Employee.class);
        return query.getResultList();
    }

//    public MapLog findMapLog(int id) {
//        return entityManager.find(MapLog.class, id);
//    }
//    public MapLogFile findMapLogFile(int id) {
//        return entityManager.find(MapLogFile.class, id);
//    }
//    public MapLogInfo findMapLogInfo(int id) {
//        return entityManager.find(MapLogInfo.class, id);
//    }
//    public MapLogTask findMapLogTash(int id) {
//        return entityManager.find(MapLogTask.class, id);
//    }
}
